<?php

// ##############################################################################||
// #                                                                 
// #   MySmartBB Version 1.7.0	                                      
// #   http://www.MySmartBB.com                                      
// #   Copyright (c) 2008 by MySmartBB team                           
// #   license http://opensource.org/licenses/gpl-license.php GNU Public License
// #                                                             
// #   filename : editsubject.php                                             
// #   edit subjects                                         
// #                                                                  
// ##############################################################################||

// ** General definitions **

     define('NOT_IN_INDEX',0);
     include('common.php');
     include('includes/SmartCode.js');

     if ($_GET['edit'] == 1)
     {
         $SF->html_title_page('تحرير موضوع - (Powered By MySmartBB Universal)');

         if ($groupper_row['edit_own_subject'] != 1)
         {
             $SF->error('المعذرة لا يمكنك تحرير المواضيع');
         }

         $id = intval($_GET['id']);
         $getsubjectinfo_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "subject WHERE id='$id'");
         $getsubjectinfo_row   = $DB->sql_fetch_array($getsubjectinfo_query);

         if (($getsubjectinfo_row['writer'] != $member_row['username']) and $groupper_row['admincp_allow']= 1)
         {
             $SF->error('المعذرة، لايمكنك تحرير هذا الموضوع');
         }

         if ($groupper_row['admincp_allow'] != 1)
         {
             $time_out = $info_row['time_out']*60;
             if ($now > $getsubjectinfo_row['write_time']+$time_out)
             {
                 $SF->error('انتهى الوقت المحدد للتعديل<br /><br /> الوقت المحدد للتعديل هو ' . $info_row['time_out'] . ' دقيقة');
             }
         }

         if ($_GET['index'] == 1)
         {
             $icons_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "smiles WHERE smile_type<>'0' AND smile_path<>'" . $getsubjectinfo_row['icon'] . "' ORDER BY id DESC");

             $icons_row = array();
             while ($icon_row = $DB->sql_fetch_array($icons_query))
             {
                 $icons_row[] = $icon_row;
                 $Smarty->assign('icons_row',$icons_row);
             }

             $smiles_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "smiles WHERE smile_type='0' ORDER BY id ASC LIMIT 0,13");

             $smiles_row = array();
             while ($smile_row = $DB->sql_fetch_array($smiles_query))
             {
                 $smiles_row[] = $smile_row;
                 $Smarty->assign('smiles_row',$smiles_row);
             }

             $text = stripslashes($getsubjectinfo_row['text']);
			 $text = htmlspecialchars($text);

             $Smarty->assign('text',$text);
             $Smarty->assign_by_ref('getsubjectinfo_row',$getsubjectinfo_row);
             $SF->GetSmartCode();

             $section_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE id = '".$getsubjectinfo_row['section']."'");
             $section_row   = $DB->sql_fetch_array($section_query);

             if ($section_row['sub_section'] == 1)
             {
                 $getmainsection_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE id = '".$section_row['from_sub_section']."'");
                 $getmainsection_row = $DB->sql_fetch_array($getmainsection_query);
                 $Smarty->assign('path_bar',"<a href=forum.php?show=1&id=".$getmainsection_row['id'].">".$getmainsection_row['title']."</a> -> <a href=forum.php?show=1&id=".$section_row['id'].">".$section_row['title']."</a> -> <a href=show.php?main=1&id=$id>".$getsubjectinfo_row['title']."</a> -> تحرير الموضوع");
             }
             else
             {
                 $Smarty->assign('path_bar',"<a href=forum.php?show=1&id=".$section_row['id'].">".$section_row['title']."</a> -> <a href=show.php?main=1&id=$id>".$getsubjectinfo_row['title']."</a> -> تحرير الموضوع");
             }

             $Smarty->display('path-bar.tpl');
             $Smarty->display('post_edit-subject-and-reply.tpl');
             $Smarty->display('footer.tpl');
         }
         elseif ($_GET['start'] == 1)
         {

             if ((strlen(trim($_POST['T1'])) <= 0) or (strlen(trim($_POST['S1'])) <= 0))
             {
                 $SF->error('يرجى تعبئة كافة المعلومات');
             }

             if (strlen(trim($_POST['T1'])) > $info_row['post_title_max'])
             {
                 $SF->error('عدد حروف عنوان الموضوع أكثر من (' . $info_row['post_title_max'] . ') حرف');
             }

             if (strlen(trim($_POST['T1'])) < $info_row['post_title_min'])
             {
                 $SF->error('عدد حروف عنوان الموضوع أقل من (' . $info_row['post_title_min'] . ') حرف');
             }

             if (strlen(trim($_POST['S1'])) > $info_row['post_text_max'])
             {
                 $SF->error('عدد حروف الموضوع أكثر من (' . $info_row['post_text_max'] . ') حرف');
             }

             if (strlen(trim($_POST['S1'])) < $info_row['post_text_min'])
             {
                 $SF->error('عدد حروف الموضوع أقل من (' . $info_row['post_text_min'] . ') حرف');
             }

             $sectionlast_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE id='" . $getsubjectinfo_row['section'] . "' AND last_subjectid='" . $getsubjectinfo_row['id'] . "'");
             $sectionlast_num   = $DB->sql_num_rows($sectionlast_query);
             $sectionlast_row   = $DB->sql_fetch_array($sectionlast_query);

             if ($sectionlast_num > 0)
             {
                 $update_last = $DB->sql_query("UPDATE " . $db_prefix . "section SET last_subject='" . $_POST['T1'] . "' WHERE id='" . $sectionlast_row['id'] . "'");
             }

             if ($sectionlast_row['sub_section'] == 1)
             {
                 $update_last = $DB->sql_query("UPDATE " . $db_prefix . "section SET last_subject='" . $SF->SafeSQL($SF->titlecut($_POST['T1'],28)) . "' WHERE id='" . $sectionlast_row['from_sub_section'] . "'");
             }

	     $update = $DB->sql_query("UPDATE " . $db_prefix . "subject SET title='" . $_POST['T1'] . "',text='" . $_POST['S1'] . "',subject_describe='" . $_POST['T2'] . "',icon='" . $_POST['R1'] . "',action_by='" . $SF->SafeSQL($member_row['username']) . "',actiondate=now(), subscribe='" . $_POST['subs'] . "', edit_reason='" . $_POST['T9'] . "' WHERE id='$id'");
             
             if ($update)
             {
                 $updateforums     =  $SF->UpdateForumsArray();
                 $updatesubforums  =  $SF->UpdateSubForums();
                 if (($updateforums) && ($updatesubforums))
                 {
					$Smarty->assign('msgg','حُدّثَت المعلومات بنجاح'); 
                	$Smarty->assign("GO","show.php?main=1&id=" . $id);
					$SF->go_to('show.php?main=1&id=' . $id,2);                  
                    $Smarty->display('loading.tpl');

                    $Smarty->display('footer.tpl');
                 }
             }
         }
     }

?>